<template>
  <div
    class="modal fade"
    tabindex="-1"
    role="dialog"
    aria-hidden="true"
    :id="id"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <template v-if="deptId === 0"> 增加部门 </template>
              <template v-else> 修改部门 </template>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row mb-2">
                <label class="control-label col-3 text-end"> 部门ID* </label>
                <div class="col-9">
                  <input type="text" class="form-control" v-model="wwDeptId" />
                  <span class="text-muted small"
                    >数字,企业微信的部门ID,0表示系统自动生成</span
                  >
                </div>
              </div>
              <div class="row mb-2">
                <label class="control-label col-3 text-end">
                  上级部门ID*
                </label>
                <div class="col-9">
                  <input type="text" class="form-control" v-model="parentId" />
                  <span class="text-muted small"
                    >数字,企业微信的上级部门ID</span
                  >
                </div>
              </div>
              <div class="row mb-2">
                <label class="control-label col-3 text-end"> 部门名称* </label>
                <div class="col-9">
                  <input type="text" class="form-control" v-model="name" />
                  <small class="form-text text-muted"></small>
                </div>
              </div>
              <div class="row mb-2">
                <label class="control-label col-3 text-end"> 部门角色* </label>
                <div class="col-9">
                  <select class="form-select" v-model="role">
                    <option
                      :value="info.role"
                      v-for="info in roles"
                      :key="info.role"
                    >
                      {{ info.roleName }}/{{ info.role }}
                    </option>
                  </select>
                  <small class="form-text text-muted"></small>
                </div>
              </div>
              <div class="row mb-2">
                <label class="control-label col-3 text-end"> 备注 </label>
                <div class="col-9">
                  <input type="text" class="form-control" v-model="remark" />
                  <small class="form-text text-muted"></small>
                </div>
              </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import $ from "jquery";

export default {
  props: {
    nullable: {
      type: Boolean,
      default: true,
    },
    id: {
      type: String,
      default: "departmentModal",
    },
    roles: {
      type: Array,
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      deptId: 0,
      name: "",
      role: "",
      parentId: 0,
      wwDeptId: 0,
      remark: "",

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.hideModal();

      this.resolve({
        id: this.deptId,
        wwDeptId: this.wwDeptId,
        name: this.name,
        role: this.role,
        parentId: this.parentId,
        remark: this.remark,
      });
    },
    modal: function (info) {
      console.log(info)
      this.deptId = info.id;
      this.wwDeptId = info.wwDeptId;
      this.name = info.name;
      this.role = info.role;
      this.parentId = info.parentId;
      this.remark = info.remark;

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
